package org.dromara.Nyt7StatisticsInsureNumCompany.service.impl;

import org.dromara.Nyt7PolicyInsurance.domain.Nyt7PolicyInsurance;
import org.dromara.Nyt7PolicyInsurance.mapper.Nyt7PolicyInsuranceMapper;
import org.dromara.utils.utils.FilterUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.dromara.Nyt7StatisticsInsureNumCompany.domain.bo.Nyt7StatisticsInsureNumCompanyBo;
import org.dromara.Nyt7StatisticsInsureNumCompany.domain.vo.Nyt7StatisticsInsureNumCompanyVo;
import org.dromara.Nyt7StatisticsInsureNumCompany.service.INyt7StatisticsInsureNumCompanyService;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * 统计分析_投保统计_数量统计_按保险公司的数量统计Service业务层处理
 *
 * @author yt
 * @date 2025-01-06
 */
@RequiredArgsConstructor
@Service
public class Nyt7StatisticsInsureNumCompanyServiceImpl implements INyt7StatisticsInsureNumCompanyService {

    private final Nyt7PolicyInsuranceMapper nyt7PolicyInsuranceMapper;
    /**
     * 分页查询统计分析_投保统计_数量统计_按保险公司的数量统计列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 统计分析_投保统计_数量统计_按保险公司的数量统计分页列表
     */
    @Override
    public TableDataInfo<Nyt7StatisticsInsureNumCompanyVo> queryPageList(Nyt7StatisticsInsureNumCompanyBo bo, PageQuery pageQuery) {

        List<Nyt7StatisticsInsureNumCompanyVo> statisticsVos = getStatisticsVos(bo);
        Page<Object> build = pageQuery.build();
        // 创建 Page 对象
        Page<Nyt7StatisticsInsureNumCompanyVo> page = new Page<>(build.getCurrent(),build.getSize());
        page.setRecords(statisticsVos);
        page.setTotal(statisticsVos.size());
        return TableDataInfo.build(page);
    }

    private @NotNull List<Nyt7StatisticsInsureNumCompanyVo> getStatisticsVos(Nyt7StatisticsInsureNumCompanyBo bo) {

        List<Nyt7PolicyInsurance> nyt7PolicyInsurances = FilterUtil.getInsurances(bo, nyt7PolicyInsuranceMapper, "company");

        if(nyt7PolicyInsurances == null) return new ArrayList<>();

        //总亩数
        BigDecimal totalMu = BigDecimal.valueOf(nyt7PolicyInsurances.stream()
            .mapToDouble(Nyt7PolicyInsurance::getTotalContractArea)
            .sum());

        List<Nyt7StatisticsInsureNumCompanyVo> statisticsVos = new ArrayList<>();

        long i = 1;
        if (!nyt7PolicyInsurances.isEmpty()) {
            for (Nyt7PolicyInsurance nyt7PolicyInsurance : nyt7PolicyInsurances) {
                //面积
                BigDecimal area = BigDecimal.valueOf(nyt7PolicyInsurance.getTotalContractArea());
                //面积占比
                // 使用 BigDecimal 进行除法运算，并指定精度和舍入模式
                BigDecimal areaRatio = area
                    .divide(totalMu, 5, RoundingMode.HALF_UP);
                Nyt7StatisticsInsureNumCompanyVo nyt7StatisticsInsureNumCompany = new Nyt7StatisticsInsureNumCompanyVo();
                nyt7StatisticsInsureNumCompany.setId(i++);
                nyt7StatisticsInsureNumCompany.setCompany(nyt7PolicyInsurance.getSysPost().getPostName());
                nyt7StatisticsInsureNumCompany.setAreaAcres(area.toString());
                nyt7StatisticsInsureNumCompany.setAreaRatio(areaRatio);
                nyt7StatisticsInsureNumCompany.setCreateTime(nyt7PolicyInsurance.getCreateTime());
                statisticsVos.add(nyt7StatisticsInsureNumCompany);
            }
        }
        return statisticsVos;
    }

    /**
     * 查询符合条件的统计分析_投保统计_数量统计_按保险公司的数量统计列表
     *
     * @param bo 查询条件
     * @return 统计分析_投保统计_数量统计_按保险公司的数量统计列表
     */
    @Override
    public List<Nyt7StatisticsInsureNumCompanyVo> queryList(Nyt7StatisticsInsureNumCompanyBo bo) {
        return getStatisticsVos(bo);
    }
}
